package org.rsbot.script.wrappers;

import java.awt.Point;
import java.util.Arrays;
import java.util.Iterator;
import org.rsbot.client.Client;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.MethodProvider;

public class RSInterface extends MethodProvider
  implements Iterable<RSComponent>
{
  private RSComponent[] childCache = new RSComponent[0];

  private final Object childLock = new Object();
  private int index;

  public RSInterface(MethodContext paramMethodContext, int paramInt)
  {
    super(paramMethodContext);
    this.index = paramInt;
  }

  public boolean containsAction(String paramString)
  {
    for (RSComponent localRSComponent : getComponents()) {
      if (localRSComponent == null) {
        continue;
      }
      if (localRSComponent.getActions() == null)
        return false;
      for (String str : localRSComponent.getActions()) {
        if (str == null) {
          continue;
        }
        if (str.toLowerCase().contains(paramString.toLowerCase())) {
          return true;
        }
      }
    }
    return false;
  }

  public boolean containsText(String paramString)
  {
    return getText().contains(paramString);
  }

  public RSComponent getComponent(int paramInt)
  {
    synchronized (this.childCache) {
      org.rsbot.client.RSInterface[] arrayOfRSInterface = getChildrenInternal();
      int i = Math.max(arrayOfRSInterface != null ? arrayOfRSInterface.length : 0, paramInt + 1);
      if (this.childCache.length < i) {
        int j = this.childCache.length;
        this.childCache = ((RSComponent[])Arrays.copyOf(this.childCache, i));
        for (int k = j; k < i; k++) {
          this.childCache[k] = new RSComponent(this.methods, this, k);
        }
      }
      return this.childCache[paramInt];
    }
  }

  public int getChildCount()
  {
    org.rsbot.client.RSInterface[] arrayOfRSInterface = getChildrenInternal();
    if (arrayOfRSInterface != null)
      return arrayOfRSInterface.length;
    return 0;
  }

  public RSComponent[] getComponents()
  {
    synchronized (this.childLock) {
      org.rsbot.client.RSInterface[] arrayOfRSInterface = getChildrenInternal();
      if (arrayOfRSInterface == null) {
        return (RSComponent[])this.childCache.clone();
      }
      if (this.childCache.length < arrayOfRSInterface.length) {
        int i = this.childCache.length;
        this.childCache = ((RSComponent[])Arrays.copyOf(this.childCache, arrayOfRSInterface.length));
        for (int j = i; j < this.childCache.length; j++) {
          this.childCache[j] = new RSComponent(this.methods, this, j);
        }
      }
      return (RSComponent[])this.childCache.clone();
    }
  }

  public int getIndex()
  {
    return this.index;
  }

  public Point getLocation()
  {
    org.rsbot.client.RSInterface[] arrayOfRSInterface1 = getChildrenInternal();
    if (arrayOfRSInterface1 != null) {
      for (org.rsbot.client.RSInterface localRSInterface : arrayOfRSInterface1) {
        if ((localRSInterface != null) && 
          (localRSInterface.getMasterX() != -1) && (localRSInterface.getMasterY() != -1)) {
          return new Point(localRSInterface.getMasterX(), localRSInterface.getMasterY());
        }
      }
    }
    return new Point(-1, -1);
  }

  public String getText()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    org.rsbot.client.RSInterface[] arrayOfRSInterface1 = getChildrenInternal();
    if (arrayOfRSInterface1 != null) {
      for (org.rsbot.client.RSInterface localRSInterface : arrayOfRSInterface1)
      {
        String str;
        if ((localRSInterface != null) && ((str = localRSInterface.getText()) != null)) {
          localStringBuilder.append(str);
          localStringBuilder.append("\r\n");
        }
      }
    }
    if (localStringBuilder.length() > 2) {
      localStringBuilder.setLength(localStringBuilder.length() - 2);
    }
    return localStringBuilder.toString();
  }

  public boolean isValid()
  {
    if (getChildrenInternal() == null) {
      return false;
    }
    int i = getIndex();
    boolean[] arrayOfBoolean = this.methods.client.getValidRSInterfaceArray();
    if ((i >= 0) && (arrayOfBoolean != null) && (i < arrayOfBoolean.length) && (arrayOfBoolean[i] != 0)) {
      org.rsbot.client.RSInterface[][] arrayOfRSInterface = this.methods.client.getRSInterfaceCache();
      if ((i < arrayOfRSInterface.length) && (arrayOfRSInterface[i] != null)) {
        RSComponent[] arrayOfRSComponent1 = getComponents();
        int j = 0;
        for (RSComponent localRSComponent : arrayOfRSComponent1) {
          if (localRSComponent.getBoundsArrayIndex() == -1) {
            j++;
          }
        }
        return j != arrayOfRSComponent1.length;
      }
    }
    return false;
  }

  public Iterator<RSComponent> iterator()
  {
    return new Iterator() {
      private int nextIdx = 0;

      public boolean hasNext() {
        return (!RSInterface.this.isValid()) && (RSInterface.this.getChildCount() >= this.nextIdx);
      }

      public RSComponent next() {
        RSComponent localRSComponent = RSInterface.this.getComponent(this.nextIdx);
        this.nextIdx += 1;
        return localRSComponent;
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }

  org.rsbot.client.RSInterface[] getChildrenInternal()
  {
    Client localClient = this.methods.client;
    if (localClient == null) {
      return null;
    }
    org.rsbot.client.RSInterface[][] arrayOfRSInterface = localClient.getRSInterfaceCache();
    if ((arrayOfRSInterface != null) && (this.index < arrayOfRSInterface.length))
      return arrayOfRSInterface[this.index];
    return null;
  }

  void setChild(RSComponent paramRSComponent) {
    synchronized (this.childLock)
    {
      int i = paramRSComponent.getIndex();
      if (i >= this.childCache.length) {
        getComponent(i);
        this.childCache[i] = paramRSComponent;
      }
    }
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
      return true;
    if ((paramObject instanceof RSInterface)) {
      RSInterface localRSInterface = (RSInterface)paramObject;
      return localRSInterface.index == this.index;
    }
    return false;
  }

  public int hashCode()
  {
    return this.index;
  }
}